```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis SQL映射机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
            color: white;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background: #f8f9fa;
            border-left: 4px solid #4b6cb7;
        }
        .highlight {
            background-color: rgba(75, 108, 183, 0.1);
            border-left: 4px solid #4b6cb7;
            padding: 0.5rem 1rem;
        }
        .first-letter {
            font-size: 3em;
            float: left;
            line-height: 0.8;
            margin-right: 0.2em;
            color: #4b6cb7;
            font-weight: bold;
        }
        .footer {
            background-color: #1a202c;
            color: #e2e8f0;
        }
        .footer a:hover {
            color: #63b3ed;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <div class="flex justify-center mb-6">
                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-white bg-opacity-20 text-white">
                    <i class="fas fa-database mr-2"></i> ORM 框架解析
                </span>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-6">MyBatis SQL映射机制深度解析</h1>
            <p class="text-xl mb-8 opacity-90 max-w-3xl mx-auto">探索MyBatis如何通过XML配置实现Java对象与数据库表的灵活映射</p>
            <div class="flex justify-center space-x-4">
                <button class="px-6 py-3 bg-white text-blue-800 font-medium rounded-lg hover:bg-gray-100 transition-all">
                    <i class="fas fa-book-open mr-2"></i> 阅读全文
                </button>
                <button class="px-6 py-3 border border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition-all">
                    <i class="fas fa-share-alt mr-2"></i> 分享
                </button>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-start">
                <span class="first-letter">S</span>
                <div>
                    <h2 class="text-2xl font-bold mb-4">SQL映射的作用与基础概念</h2>
                    <p class="mb-4">SQL映射是MyBatis的核心功能之一，它通过XML文件配置SQL语句，并在Java代码中执行映射操作。MyBatis支持通过XML或者注解两种方式定义SQL语句，这里我们重点关注基于XML配置的SQL映射。</p>
                    <p>MyBatis的SQL映射文件（通常为<code class="bg-gray-100 px-1 py-0.5 rounded">Mapper.xml</code>）定义了与数据库交互的SQL语句，具体的映射操作如查询、插入、更新等。通过SQL映射，Java对象与数据库表之间可以灵活地进行转换。</p>
                </div>
            </div>
        </section>

        <!-- SqlSession Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="flex-shrink-0 mr-4 text-blue-600">
                    <i class="fas fa-cog text-3xl"></i>
                </div>
                <h2 class="text-2xl font-bold">SqlSession的初始化过程</h2>
            </div>
            <p class="mb-6"><code class="bg-gray-100 px-1 py-0.5 rounded">SqlSession</code>是MyBatis与数据库交互的基础，负责执行SQL语句并返回结果。它的初始化过程决定了SQL映射的正确执行。我们首先看一下<code class="bg-gray-100 px-1 py-0.5 rounded">SqlSessionFactory</code>的创建过程。</p>
            
            <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                <pre><code class="text-sm">// 创建 SqlSessionFactory 的关键方法
public SqlSessionFactory build(Reader reader) throws IOException {
    XMLConfigBuilder parser = new XMLConfigBuilder(reader);
    Configuration configuration = parser.parse(); // 解析 XML 配置文件
    return new DefaultSqlSessionFactory(configuration);
}</code></pre>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="font-bold text-lg mb-3 flex items-center">
                        <i class="fas fa-code-branch text-blue-500 mr-2"></i> XMLConfigBuilder解析
                    </h3>
                    <p>在这个过程中，<code class="bg-gray-100 px-1 py-0.5 rounded">XMLConfigBuilder</code>负责解析MyBatis的全局配置XML文件（如<code class="bg-gray-100 px-1 py-0.5 rounded">mybatis-config.xml</code>），并通过<code class="bg-gray-100 px-1 py-0.5 rounded">parse()</code>方法将其转化为<code class="bg-gray-100 px-1 py-0.5 rounded">Configuration</code>对象。</p>
                </div>
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="font-bold text-lg mb-3 flex items-center">
                        <i class="fas fa-cogs text-blue-500 mr-2"></i> Configuration对象
                    </h3>
                    <p><code class="bg-gray-100 px-1 py-0.5 rounded">Configuration</code>对象包含了MyBatis框架的所有配置信息，其中就包括了SQL映射的XML配置。</p>
                </div>
            </div>
        </section>

        <!-- XML解析流程 -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold mb-6 flex items-center">
                <i class="fas fa-sitemap text-blue-500 mr-3"></i> XML解析流程
            </h2>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4">XMLConfigBuilder与XML解析</h3>
                <p class="mb-4"><code class="bg-gray-100 px-1 py-0.5 rounded">XMLConfigBuilder</code>是MyBatis用于解析配置文件的核心类，负责将XML配置文件中的内容转换为<code class="bg-gray-100 px-1 py-0.5 rounded">Configuration</code>对象。我们来看一下<code class="bg-gray-100 px-1 py-0.5 rounded">parse()</code>方法的源码实现。</p>
                
                <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                    <pre><code class="text-sm">public Configuration parse() throws IOException {
    try {
        XNode root = document.getRootElement();
        parseConfiguration(root);
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Map configuration.", e);
    }
    return configuration;
}</code></pre>
                </div>
                
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="highlight">
                        <i class="fas fa-info-circle text-blue-500 mr-2"></i> <code class="font-medium">getRootElement()</code>方法获取XML文件的根元素（通常是<code>&lt;configuration&gt;</code>）。
                    </div>
                    <div class="highlight">
                        <i class="fas fa-info-circle text-blue-500 mr-2"></i> <code class="font-medium">parseConfiguration()</code>方法开始解析具体的配置内容，解析过程中会逐个读取如<code>&lt;mappers&gt;</code>、<code>&lt;settings&gt;</code>等配置项，并构建出<code>Configuration</code>对象。
                    </div>
                </div>
            </div>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4">Mapper.xml中SQL映射语句的解析</h3>
                <p class="mb-4"><code class="bg-gray-100 px-1 py-0.5 rounded">Mapper.xml</code>文件中每一个SQL语句都通过特定的标签进行定义，如<code>&lt;select&gt;</code>、<code>&lt;insert&gt;</code>、<code>&lt;update&gt;</code>等。这些标签会被<code class="bg-gray-100 px-1 py-0.5 rounded">XMLMapperBuilder</code>类解析。</p>
                
                <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                    <pre><code class="text-sm">// XMLMapperBuilder中的核心解析方法
public void parse() throws Exception {
    XNode context = root;
    String namespace = context.getStringAttribute("namespace");
    // 解析每个 SQL 映射语句
    buildStatementFromContext(context, namespace);
}</code></pre>
                </div>
                
                <div class="highlight">
                    <i class="fas fa-info-circle text-blue-500 mr-2"></i> <code class="font-medium">buildStatementFromContext()</code>方法负责解析具体的SQL语句，每一个<code>&lt;select&gt;</code>、<code>&lt;insert&gt;</code>等标签都会通过这个方法被转换成相应的<code>MappedStatement</code>对象。
                </div>
            </div>
        </section>

        <!-- MappedStatement Section -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold mb-6 flex items-center">
                <i class="fas fa-project-diagram text-blue-500 mr-3"></i> MappedStatement的构建
            </h2>
            
            <p class="mb-6"><code class="bg-gray-100 px-1 py-0.5 rounded">MappedStatement</code>是MyBatis映射SQL语句的核心对象，它包含了SQL语句的相关信息以及如何执行该SQL语句的配置信息。</p>
            
            <div class="code-block p-4 rounded-lg mb-6 overflow-x-auto">
                <pre><code class="text-sm">// MappedStatement构建过程
public MappedStatement.Builder builder(String namespace, XNode context) {
    String id = context.getStringAttribute("id");
    String statementType = context.getStringAttribute("statementType");
    SqlCommandType sqlCommandType = SqlCommandType.valueOf(statementType.toUpperCase());
    
    // 根据标签类型构建具体的SQL执行逻辑
    MappedStatement.Builder builder = new MappedStatement.Builder(configuration, id, sqlCommandType, context);
    return builder;
}</code></pre>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="font-bold text-lg mb-3 flex items-center">
                        <i class="fas fa-tag text-blue-500 mr-2"></i> SQL语句标识
                    </h3>
                    <p><code class="bg-gray-100 px-1 py-0.5 rounded">id</code>属性唯一标识一个SQL语句，通过命名空间+id可以准确定位到具体的SQL映射。</p>
                </div>
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="font-bold text-lg mb-3 flex items-center">
                        <i class="fas fa-terminal text-blue-500 mr-2"></i> SQL类型
                    </h3>
                    <p><code class="bg-gray-100 px-1 py-0.5 rounded">statementType</code>决定了SQL语句的类型（SELECT、INSERT、UPDATE等），影响最终的执行方式。</p>
                </div>
            </div>
        </section>

        <!-- Execution Flow -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold mb-6 flex items-center">
                <i class="fas fa-random text-blue-500 mr-3"></i> 执行流程与参数绑定
            </h2>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4">Executor的工作流程</h3>
                <p class="mb-4"><code class="bg-gray-100 px-1 py-0.5 rounded">Executor</code>是MyBatis的核心执行器，负责执行SQL语句并返回结果。在<code class="bg-gray-100 px-1 py-0.5 rounded">SqlSession</code>中，我们调用<code class="bg-gray-100 px-1 py-0.5 rounded">Executor</code>的<code class="bg-gray-100 px-1 py-0.5 rounded">query()</code>方法来执行查询操作。</p>
                
                <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                    <pre><code class="text-sm">// 执行SQL语句的关键方法
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) {
    // 通过MappedStatement找到对应的SQL语句
    BoundSql boundSql = ms.getBoundSql(parameter);
    // 执行SQL语句
    return query(ms.getSqlSession().getConnection(), boundSql, parameter, rowBounds, resultHandler);
}</code></pre>
                </div>
                
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="highlight">
                        <i class="fas fa-link text-blue-500 mr-2"></i> <code class="font-medium">BoundSql</code>是封装SQL语句的对象，它包含了最终执行的SQL语句和相关的参数。
                    </div>
                    <div class="highlight">
                        <i class="fas fa-database text-blue-500 mr-2"></i> <code class="font-medium">query()</code>方法会将SQL语句传递给JDBC进行执行，并通过<code>ResultHandler</code>将结果映射为Java对象。
                    </div>
                </div>
            </div>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4">SQL语句的参数绑定</h3>
                <p class="mb-4">在SQL映射中，参数绑定是一个重要的步骤。MyBatis通过<code class="bg-gray-100 px-1 py-0.5 rounded">BoundSql</code>来处理SQL语句和参数的绑定。</p>
                
                <div class="code-block p-4 rounded-lg overflow-x-auto">
                    <pre><code class="text-sm">// BoundSql对象处理参数绑定的关键代码
public BoundSql getBoundSql(Object parameterObject) {
    // 根据传入的参数，生成最终的SQL语句
    String sql = sqlSource.getBoundSql(parameterObject).getSql();
    return new BoundSql(configuration, sql, parameterMappings, parameterObject);
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Summary & Diagram -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold mb-6 flex items-center">
                <i class="fas fa-sitemap text-blue-500 mr-3"></i> 整体流程总结
            </h2>
            
            <p class="mb-6">从上面的源码分析，我们可以总结出MyBatis中SQL映射与XML解析的整体执行流程：</p>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-blue-500 text-3xl mb-3">
                        <i class="fas fa-file-code"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">1. 配置加载与解析</h3>
                    <p>MyBatis通过<code class="bg-gray-100 px-1 py-0.5 rounded">XMLConfigBuilder</code>加载全局配置文件，并解析出数据库连接信息和映射配置。</p>
                </div>
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-blue-500 text-3xl mb-3">
                        <i class="fas fa-file-alt"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">2. Mapper文件解析</h3>
                    <p>通过<code class="bg-gray-100 px-1 py-0.5 rounded">XMLMapperBuilder</code>解析<code class="bg-gray-100 px-1 py-0.5 rounded">Mapper.xml</code>文件，将SQL映射语句转化为<code class="bg-gray-100 px-1 py-0.5 rounded">MappedStatement</code>。</p>
                </div>
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-blue-500 text-3xl mb-3">
                        <i class="fas fa-play"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">3. SQL执行</h3>
                    <p><code class="bg-gray-100 px-1 py-0.5 rounded">SqlSession</code>通过<code class="bg-gray-100 px-1 py-0.5 rounded">Executor</code>执行SQL，查询结果返回给用户。</p>
                </div>
            </div>
            
            <div class="bg-white p-6 rounded-lg">
                <div class="mermaid">
                    sequenceDiagram
                        participant User
                        participant SqlSession
                        participant Executor
                        participant MappedStatement
                        participant JDBC
                        
                        User->>SqlSession: 调用查询方法
                        SqlSession->>Executor: 执行查询(query)
                        Executor->>MappedStatement: 获取BoundSql
                        MappedStatement-->>Executor: 返回BoundSql
                        Executor->>JDBC: 执行SQL查询
                        JDBC-->>Executor: 返回结果集
                        Executor-->>SqlSession: 返回结果对象
                        SqlSession-->>User: 返回查询结果
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="footer py-8 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-lg font-medium">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:underline">http://www.yuque.com/jtostring</a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```